
import { globalUtils } from '../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';
import uiData from 'ui-data';
import { CurrentRunningApp } from '../../common/js/current-running-app';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewMain('profile-detail', function (scope, view) {
  var dataModel = view.getDataModel();
  var jqTable = view.$ui('profileSettingTable');
  var jqBackBtn = view.$ui('backBtn');
  var jqEditDialog = view.$ui('editProfileSettingDialog');
  var jqEditForm = jqEditDialog.find('form');

  var urlParams = view.getUrlParams();
  var profileItemId = urlParams.id;

  // 初始表格
  uiUtils.dataGrid.init(jqTable, {
    columns: [
      {
        field: 'id'
      },
      {
        field: 'levelTypeDesc'
      },
      {
        field: 'valueCode'
      },
      {
        field: 'remark'
      },
      {
        field: 'enableFlagDesc'
      },
      {
        field: 'operate',
        'class': 'x-nowrap-items',
        formatter: formatOprationColumn,
        events: {
          'click .edit': clickEditOperationFn
        }
      }
    ]
  });

  function formatOprationColumn(value, row, index) {
    if (row.readonlyFlag) {
      return [
        '<a class="edit" href="javascript:void(0)" disabled>',
        '<span>${t("ui.system.common.operation.edit")}</span>',
        '</a>'
      ].join('');
    } else {
      return [
        '<a class="edit" href="javascript:void(0)">',
        '<span>${t("ui.system.common.operation.edit")}</span>',
        '</a>'
      ].join('');
    }
  }

  // 加载数据
  loadOrReloadData();

  /**
   * 加载数据
   */
  function loadOrReloadData() {
    var queryParams = {
      id: profileItemId
    };

    // 调用接口获取记录
    view.callService('api/infrastructure/profile/getProfileDetailById', queryParams)
      .waitResult(function (result, textStatus, jqXHR) {
        if (!uiUtils.common.checkWebResultSuccess(result)) {
          return;
        }

        var data = result.data;
        // 表格数据
        var profileSettings = [globalUtils.concatObjects({ id: 1 }, data.appSetting),
        globalUtils.concatObjects({ id: 2 }, data.tenantSetting),
        globalUtils.concatObjects({ id: 3 }, data.userSetting)
        ];

        dataModel.setData('data.*', uiData.utils.prefix({
          profileItemCode: data.profileItemCode,
          profileItemName: data.profileItemName,
          profileItemDesc: data.profileItemDesc,
          profileSettings: profileSettings
        }, "data."));
      });
  }

  // 点击"返回"按钮的事件处理
  jqBackBtn.on(uiUtils.event.CLICK, function () {
    Pure.app.popView('app/infrastructure/profile/management');
  });

  //
  // "编辑值项"弹出框
  //

  jqEditForm.validate({
    rules: {
      valueCode: {
        maxlength: 100
      },
      remark: {
        maxlength: 200
      }
    }
  });

  // 点击"编辑值项"弹出框的保存按钮
  jqEditDialog.on(uiUtils.event.CLICK, uiUtils.dialog.BTN_CONFIRM, function () {
    var checkResult = jqEditForm.valid();
    if (!checkResult) {
      return;
    }

    var data = dataModel.getData('edit.*');
    data = uiData.utils.unprefix(data, 'edit.');
    data = globalUtils.concatObjects({ profileItemId: profileItemId }, data);

    // 调用更新记录的接口
    view.callService('api/infrastructure/profile/updateProfileValue', data)
      .waitResult(function (result) {
        if (result.success) {
          uiUtils.dialog.hideModal(jqEditDialog);
          Pure.app.showMessage('${t("ui.system.common.msg.saveRecordSuccess")}');
          // 加载数据
          loadOrReloadData();
        } else {
          Pure.app.showError(result.message);
        }
      });
  });

  jqEditDialog.on(uiUtils.event.MODAL_HIDE, function () {
    uiUtils.view.notCheckFormDataChangeWhenLeave(scope);
  });

  function getEditData() {
    return dataModel.getData('edit.*');
  }

  //
  //
  //

  /**
   * 点击"编辑"操作的事件
   * @param {*} e 
   * @param {*} value 
   * @param {*} row 
   * @param {*} index 
   */
  function clickEditOperationFn(e, value, row, index) {
    uiUtils.dialog.showModal(jqEditDialog, {
      backdropParent: view.getViewElement(),
      onModalHide: function () {
        uiUtils.common.resetValidation(jqEditForm);
      }
    });


    var data = uiData.utils.prefix(row, 'edit.')
    dataModel.setData('edit.*', data);

    // 离开页面时检查是否表单发生变更
    uiUtils.view.setCurrentFormDataToContext(scope, getEditData);
    uiUtils.view.checkFormDataChangeWhenLeave(scope, getEditData);
  }

});

/* SOURCE-CODE-END */
